﻿using System;
using System.IO;

namespace Jxym.Tools.Converts
{
    public class OfficeConvert
    {
        public enum wdConvertType
        {
            wdFormatDocument97, wdFormatHTML, wdFormatPDF, wdFormatText, wdFormatXML, wdFormatXPS
        }
        public enum xlConvertType
        {
            xlTypePDF, xlTypeXPS
        }
        public enum ppConvertType
        {
            ppSaveAsBMP, ppSaveAsGIF, ppSaveAsHTML, ppSaveAsJPG, ppSaveAsPDF, ppSaveAsPNG, ppSaveAsTIF, ppSaveAsXPS
        }

        private Microsoft.Office.Interop.Word.WdSaveFormat wdtype;
        public Microsoft.Office.Interop.Word.WdSaveFormat wdType
        {
            get { return wdtype; }
            set { wdtype = value; }
        }

        private Microsoft.Office.Interop.Excel.XlFixedFormatType xltype;
        public Microsoft.Office.Interop.Excel.XlFixedFormatType xlType
        {
            get { return xltype; }
            set { xltype = value; }
        }

        private Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType pptype;
        public Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType ppType
        {
            get { return pptype; }
            set { pptype = value; }
        }

        private string sourcePath;
        public string InPath
        {
            get { return sourcePath; }
            set { sourcePath = value; }
        }

        private string targetPath;
        public string OutPath
        {
            get { return targetPath; }
            set { targetPath = value; }
        }

        public OfficeConvert(string InPath, string OutPath)
        {
            this.sourcePath = InPath;
            this.targetPath = OutPath;
        }

        public bool WordSaveAs(wdConvertType exportFormat)
        {
            bool result = false;
            object paramMissing = Type.Missing;
            Microsoft.Office.Interop.Word.ApplicationClass wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
            Microsoft.Office.Interop.Word.Document wordDocument = null;
            try
            {
                object paramSourceDocPath = sourcePath;
                object paramExportFilePath = targetPath;

                Microsoft.Office.Interop.Word.WdSaveFormat paramExportFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
                switch (exportFormat)
                {
                    case wdConvertType.wdFormatDocument97:
                        paramExportFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument97;
                        break;
                    case wdConvertType.wdFormatHTML:
                        paramExportFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
                        break;
                    case wdConvertType.wdFormatPDF:
                        paramExportFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
                        break;
                    case wdConvertType.wdFormatText:
                        paramExportFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatText;
                        break;
                    case wdConvertType.wdFormatXML:
                        paramExportFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXML;
                        break;
                    case wdConvertType.wdFormatXPS:
                        paramExportFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXPS;
                        break;
                }

                wordDocument = wordApplication.Documents.Open(
                        ref paramSourceDocPath, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing);

                if (wordDocument != null)
                {
                    wordDocument.SaveAs(ref paramExportFilePath, paramExportFormat, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing,
                             ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing);
                }
                result = true;
            }
            catch (Exception) { }
            finally
            {
                if (wordDocument != null)
                {
                    wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordDocument = null;
                }
                if (wordApplication != null)
                {
                    wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordApplication = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }

        public bool ExcelSaveAs(xlConvertType targetType)
        {
            bool result;
            object missing = Type.Missing;
            Microsoft.Office.Interop.Excel.ApplicationClass application = null;
            Microsoft.Office.Interop.Excel.Workbook workBook = null;
            try
            {
                application = new Microsoft.Office.Interop.Excel.ApplicationClass();
                object target = targetPath;
                object type = targetType;

                Microsoft.Office.Interop.Excel.XlFixedFormatType paramExportFormat = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
                switch (targetType)
                {
                    case xlConvertType.xlTypePDF:
                        paramExportFormat = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
                        break;
                    case xlConvertType.xlTypeXPS:
                        paramExportFormat = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypeXPS;
                        break;
                }

                workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
                        missing, missing, missing, missing, missing, missing, missing, missing, missing);
                workBook.SaveAs(targetPath, paramExportFormat, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (workBook != null)
                {
                    workBook.Close(true, missing, missing);
                    workBook = null;
                }
                if (application != null)
                {
                    application.Quit();
                    application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }

        public bool PptSaveAs(ppConvertType targetFileType)
        {
            bool result;
            object missing = Type.Missing;
            Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null;
            Microsoft.Office.Interop.PowerPoint.Presentation persentation = null;
            try
            {
                Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType paramExportFormat = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsBMP;
                switch (targetFileType)
                {
                    case ppConvertType.ppSaveAsBMP:
                        paramExportFormat = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsBMP;
                        break;
                    case ppConvertType.ppSaveAsGIF:
                        paramExportFormat = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsGIF;
                        break;
                    case ppConvertType.ppSaveAsHTML:
                        paramExportFormat = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML;
                        break;
                    case ppConvertType.ppSaveAsJPG:
                        paramExportFormat = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsJPG;
                        break;
                    case ppConvertType.ppSaveAsPDF:
                        paramExportFormat = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
                        break;
                    case ppConvertType.ppSaveAsPNG:
                        paramExportFormat = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPNG;
                        break;
                    case ppConvertType.ppSaveAsTIF:
                        paramExportFormat = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsTIF;
                        break;
                    case ppConvertType.ppSaveAsXPS:
                        paramExportFormat = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsXPS;
                        break;
                }
                application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
                persentation = application.Presentations.Open(sourcePath, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
                persentation.SaveAs(targetPath, paramExportFormat, Microsoft.Office.Core.MsoTriState.msoTrue);
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (persentation != null)
                {
                    persentation.Close();
                    persentation = null;
                }
                if (application != null)
                {
                    application.Quit();
                    application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }
    }
}
